const { list } = require('./src/mock')
const bodyParser = require('body-parser')
const userData = [
    {
        user: "kkk",
        pass: "123",
        name: "wxk",
        desc: "八维第一帅",
        image: "https://img0.baidu.com/it/u=3144486528,3918230326&fm=26&fmt=auto&gp=0.jpg"
    },
    {
        user: "myx",
        pass: "123",
        name: "myx",
        desc: "八维第一衰",
        image: "https://img1.baidu.com/it/u=975936171,820269061&fm=26&fmt=auto&gp=0.jpg"
    }
]
module.exports = {
    devServer: {
        before: (app) => {
            app.use(bodyParser.json())
            // 获取
            app.get('/list', (req, res) => {
                res.send({ list })
            })
            // 收藏
            app.post('/like', (req, res) => {
                const { id, like } = req.body;
                list.forEach(item => {
                    if (item.id === id) {
                        item.isLike = like
                    }
                })
                res.send({
                    code: 1,
                    message: "修改成功"
                })
            })
            // 删除
            app.delete('/list/:id', (req, res) => {
                const { id } = req.params;
                list.forEach(item => {
                    if (item.id == id) {
                        item.isDelete = !item.isDelete
                    }
                })
                res.send({
                    code: 1,
                    message: "删除成功"
                })
            })
            // 添加项目
            app.post('/list', (req, res) => {
                const data = req.body;
                list.unshift(data)
                res.send({
                    code: 1,
                    message: "添加成功 "
                })
            })
            // 更新项目
            app.put('/list', (req, res) => {
                const { id } = req.body;
                const ind = list.findIndex(item => item.id == id)
                list[ind] = req.body;
                res.send({
                    code: 1,
                    message: "更新成功"
                })
            })
            // 登录
            app.post('/login', (req, res) => {
                const { user, pass } = req.body;
                const isHaveUser = userData.find(item => {
                    if (item.user == user && item.pass == pass) {
                        return item
                    }
                })
                if (isHaveUser) {
                    res.send({
                        code: 1,
                        data: isHaveUser
                    })
                } else {
                    res.send({
                        code: -1,
                        data: '请检查账号密码'
                    })
                }

            })
            // 编辑个人信息
            app.put('/user', (req, res) => {
                const { user, name, desc, image } = req.body;
                let ind = userData.findIndex(item => item.user == user);
                if (ind !== -1) {
                    userData[ind] = { ...userData[ind], ...req.body };
                    res.send({
                        code: 1,
                        message: "编辑成功",
                        data: userData[ind]
                    })
                } else {
                    res.send({
                        code: -1,
                        message: "编辑失败"
                    })
                }
            })
        },
        proxy: {
            // 同源策略是客户端的策略 要求 连个端必须 协议 端口 域名 全部相等才可以互相访问
            // http 协议 8080 端口  baidu localhost 域名
            // 代理跨域就是启动一个服务器去访问接口  因为服务器和服务器直接不存在同源策略  同源策略只存在于浏览器
            "/api": { // 只要匹配到api的接口都会自动代理到 target上
                target: "http://182.254.220.243:7001",
                pathRewrite: {
                    '^/api': '' // 将api替换为空  因为api只是个别名
                }
            },
        }
    }
}